package com.xxl.javaWork_15;

/**
 * @Author: xiaxinlin
 * @Date: 2020/12/15 18:04
 */
public class AccountTest {
    private static final int MAX_OF_THREAD=1000;
    public static void main(String[] args) {
        Account account = new Account("Juliz",1000.0f);
        Thread[] threads=new Thread[MAX_OF_THREAD];
        for (int i=0;i<MAX_OF_THREAD;i++){
            threads[i]=new Thread(new Runnable() {
                @Override
                public void run() {
                    account.deposit(100);
                    account.withdraw(100);
                }
            });
            threads[i].start();
        }
        for (int i=0;i<MAX_OF_THREAD;i++){
            try {
                threads[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(account.getAmount());
    }
}

class Account{
    private String name;
    private float amount;

    public Account(String name, float amount) {
        this.name = name;
        this.amount = amount;
    }

    public String getName() {
        return name;
    }

    public float getAmount() {
        return amount;
    }

    public synchronized void deposit(float amt){
        amount=amount+amt;
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void withdraw(float amt){
        amount = amount-amt;
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
